package com.xywy.bloodandroid.act;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

public class ComfrimCodeAct extends BaseActivity {
	private RelativeLayout left_img;
	private TextView middle_title;
	private TextView time_countdown;
	private String phonenum;
	private String accountstr;
	private String password;
	private RelativeLayout binding;
	private EditText input_phone;
	private TextView binding_text;
	private TextView phone_number;
	private int flag;
	private String target;
	private String id;
	@Override
	protected void initView(Bundle savedInstanceState) {
     setContentView(R.layout.act_confirm_code);
       left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(R.string.confrim);
		time_countdown = (TextView)findViewById(R.id.time_countdown);
		binding = (RelativeLayout)findViewById(R.id.binding);
		input_phone = (EditText)findViewById(R.id.input_phone);
		binding_text = (TextView)findViewById(R.id.binding_text);
		phone_number = (TextView)findViewById(R.id.phone_number);
		flag = getIntent().getIntExtra("flag", -1);
		
		if(flag==1){
			target = getIntent().getStringExtra("target");
			id = getIntent().getStringExtra("id");//
			binding_text.setText(getResources().getString(R.string.set_new_password));;
			phone_number.setText(target);
		}
		
		
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		binding.setOnClickListener(this);
	        
	        new CountDownTimer(60000, 1000) {

				public void onTick(long millisUntilFinished) {

					time_countdown.setText("接收短信大约需要" + millisUntilFinished
							/ 1000 + "秒");
				}

				public void onFinish() {
					ComfrimCodeAct.this.finish();
				}
			}.start();
	}

	@Override
	protected void initData() {
		phonenum = getIntent().getStringExtra("phonenum");
		accountstr = getIntent().getStringExtra("accountstr");
		password = getIntent().getStringExtra("password");
	}
     @Override
    public void onClick(View v) {
    	super.onClick(v);
    	switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.binding:
			if(flag==1){
				SendForgetPasswordCode();
			}
			else{
			SendPhoneRegistCode();
			}
			break;
		default:
			break;
		}
    }

	private void SendForgetPasswordCode() {
		final String code=input_phone.getText().toString().trim();
		GetRequest<String> getRequest=new GetRequest<String>(Constants.getUrl("sms", "verifyCode")+"&target="+target+"&code="+code+"&type=2&PHPSESSID="+id, String.class, 
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context, context.getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(ComfrimCodeAct.this, waittingDialog);
						try {
							JSONObject jsonObject=new JSONObject(response) ;
							int code1=(int) jsonObject.optInt("code");
							String error=(String) jsonObject.optString("error");
							JSONObject data= jsonObject.optJSONObject("data");
							String userid=data.optString("userid");
							String phonenumber=data.optString("phonenumber");
							 if(code1==10000){
							
							 User.getInstance(context).setUserid(userid);
							 User.getInstance(context).setPhonenum(phonenumber);
							 Intent intent=new Intent(context, SetNewPasswordAct.class);
							 intent.putExtra("target", target);
							 intent.putExtra("code", code);
							 startActivity(intent);
							 ComfrimCodeAct.this.finish();
						 }else{
							 
							 UIUtil.showToast(context, error);
						 }
						} catch (JSONException e) {
							e.printStackTrace();
						}
						
						

					}
				});
		getRequest.setIsParseJson(false);//�Լ�����
		executeRequest(getRequest);
	}

	private void SendPhoneRegistCode() {
//		waitingDialog(ComfrimCodeAct.this, "���ڷ���~");
		 final String code=input_phone.getText().toString().trim();
		 PostRequest<User> postRequest=new PostRequest<User>(Constants.getUrl("kbb", "users")+"&type=registAccount",
				User.class, new OnVolleyResponseListener<User>() {

					@Override
					public void onError(VolleyError volleyError) {
						 System.out.println(".....VolleyError"+volleyError);
						 UIUtil.DissDialog(context, waittingDialog);
						 UIUtil.showToast(context, getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(User response) {
   						     System.out.println("User....."+response);
   						  UIUtil.DissDialog(ComfrimCodeAct.this, waittingDialog );
   						  
   						   if(response.getState()==200){
   							   Intent intent=new Intent(context, MainFragmentAct.class);
   							    startActivity(intent);
   							    ComfrimCodeAct.this.finish();
   						   }else{
   							UIUtil.showToast(context, response.getError());  
   						   }
   						     
					}
				});
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("phonenum", phonenum);
		params.put("password", password);
		params.put("accountstr", accountstr);
		params.put("code", code);
		params.put("sex", "2");
		params.put("height", "0");
		params.put("birthday", System.currentTimeMillis()+"");
		postRequest.setParams(params);
		executeRequest(postRequest);
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		VolleyManager.cancelRequest(this);
	}
}
